#### Monthly-level analysis that propagates the prediction uncertainty ####

source("./code/loadPackages.R")      # Load and install necessary packages
source("./code/regressIter.R")    # Code to run a separate regression for each bootstrap iteration and aggregate results  

# Load in the data
load("./data/monthlyDataIter.RData")


# A function to help resolve some errors after running parallel processes
unregister_dopar <- function() {
  env <- foreach:::.foreachGlobals
  rm(list=ls(name=env), pos=env)
}

# Set up cores for parallel processing
no_cores = detectCores() - 1
cl = makeCluster(no_cores)
registerDoParallel(cl)


# Define control variables
controlsMonth = "+ war.now + log.last.war.dead.pc + log.months.since.last.war +
  last.war.win + n.mid.us.challenge.past5 + prop.mid.win.us.past5 + is.recession + unigov + 
  cinc + log(tenuremonth)"

# Simple monthly analysis
monthFit1a = monthIter(controls="")
monthFit1a

# Monthly analysis with controls
monthFit1b = monthIter(controls=controlsMonth)
monthFit1b

# Stop the clusters
stopCluster(cl)
unregister_dopar()

#### Table A26: Effect of NSC Principals' Hawkishness on MIDs, Using Monthly Data, Propagating Uncertainty from Bootstrapping ####
makeMonthFullCoefTable(list(monthFit1a, monthFit1b))